package com.abbas.abchat.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.R;
import com.abbas.abchat.adapter.HomeVpAdapter;
import com.abbas.abchat.fragment.ChatFragment;
import com.abbas.abchat.fragment.FriendRoundFragment;
import com.abbas.abchat.fragment.MineFragment;
import com.abbas.abchat.fragment.PeopleFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity implements View.OnClickListener {

    private String CHAT_YAG = "AbChat";
    private String PEOPLE_TAG = "People";
    private String FRIEND_TAG = "Friend";

    private RelativeLayout bar;
    private TextView title;
    private ImageButton add;
    private ImageButton search;
    private BottomNavigationView nvi;
    private ViewPager vp;

    private HomeVpAdapter homeVpAdapter;
    private List<Fragment> pages = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void init() {
        bar = (RelativeLayout) findViewById(R.id.home_bar);
        title = (TextView) findViewById(R.id.title);
        add = (ImageButton) findViewById(R.id.add);
        search = (ImageButton) findViewById(R.id.search);
        nvi = (BottomNavigationView) findViewById(R.id.nvi);
        vp = (ViewPager) findViewById(R.id.vp);

        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0x01);
        }

        search.setOnClickListener(this);
        add.setOnClickListener(this);

        pages.add(ChatFragment.newInstance(null, null));
        pages.add(PeopleFragment.newInstance(null, null));
        pages.add(FriendRoundFragment.newInstance(null, null));
        pages.add(MineFragment.newInstance(null, null));

        homeVpAdapter = new HomeVpAdapter(getSupportFragmentManager(), this, pages);

        vp.setAdapter(homeVpAdapter);
//        vp.setScrollContainer(false);

        title.setText(CHAT_YAG);

        String test = AbChatApplication.getAccount().toString();
        if (!TextUtils.isEmpty(test)) {
            showMessage(test);
        }
    }

    @Override
    protected void initEvent() {
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        nvi.setSelectedItemId(R.id.chat);
                        if (bar.getVisibility() == View.INVISIBLE) bar.setVisibility(View.VISIBLE);
                        title.setText(CHAT_YAG);
                        break;
                    case 1:
                        nvi.setSelectedItemId(R.id.people);
                        if (bar.getVisibility() == View.INVISIBLE) bar.setVisibility(View.VISIBLE);
                        title.setText(PEOPLE_TAG);
                        break;
                    case 2:
                        nvi.setSelectedItemId(R.id.round);
                        if (bar.getVisibility() == View.INVISIBLE) bar.setVisibility(View.VISIBLE);
                        title.setText(FRIEND_TAG);
                        break;
                    case 3:
                        nvi.setSelectedItemId(R.id.mine);
                        bar.setVisibility(View.INVISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        nvi.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.chat:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.people:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.round:
                        vp.setCurrentItem(2);
                        break;
                    case R.id.mine:
                        vp.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search:
                migrateTo(SearchAccountActivity.class);
                break;
            case R.id.add:
                break;
        }
    }
}